package love.lkzy.oj.codeSandBox.utils;

import cn.hutool.core.util.StrUtil;
import love.lkzy.oj.codeSandBox.model.ProcessResponse;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

public class RunProcessUtil {

    /**
     * 运行Process
     *
     * @param process 过程
     * @return {@link ProcessResponse}
     */
    public static ProcessResponse runProcess(Process process) {
        ProcessResponse res = new ProcessResponse();
        try {
            int exitValue = process.waitFor();
            res.setExitValue(exitValue);

            if (exitValue == 0) {
                //System.out.println("命令执行成功 success:");
                //    获取控制台输出(这里就是输入流)
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                StringBuilder temp = new StringBuilder();
                String curLine;
                while ((curLine = reader.readLine()) != null) {
                    temp.append(curLine);
                }
                res.setSuccessMsg(temp.toString());
                reader.close();
            } else {
                //System.out.println("命令执行失败 default:" + exitValue);
                //    获取控制台输出
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                StringBuilder temp = new StringBuilder();
                String curLine;
                while ((curLine = reader.readLine()) != null) {
                    temp.append(curLine);
                }
                res.setErrorMsg(temp.toString());
                reader.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 交互式运行Process
     *
     * @param process 过程
     * @return {@link ProcessResponse}
     */
    public static ProcessResponse runInteractProcess(Process process, String arg) {
        ProcessResponse res = new ProcessResponse();
        try {

            // 定义writer用于将输入逐个投喂给程序
            OutputStreamWriter writer = new OutputStreamWriter(process.getOutputStream());
            //BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String[] argLines = arg.split("\n");
            for (String line : argLines) {
                writer.write(line + System.lineSeparator());
                writer.flush();
            }
            int exitValue = process.waitFor();
            res.setExitValue(exitValue);

            writer.close();
            if (exitValue == 0) {
                //System.out.println("命令执行成功 success:");
                //    获取控制台输出(这里就是输入流)
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                StringBuilder temp = new StringBuilder();

                String curLine;
                while ((curLine = reader.readLine()) != null) {
                    temp.append(curLine);
                    temp.append(System.lineSeparator());
                }
                temp.deleteCharAt(temp.length() - 1);
                res.setSuccessMsg(temp.toString());
                reader.close();
            } else {
                //System.out.println("命令执行失败 default:" + exitValue);
                //    获取控制台输出
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                StringBuilder temp = new StringBuilder();
                String curLine;
                while ((curLine = reader.readLine()) != null) {
                    temp.append(curLine);
                }
                res.setErrorMsg(temp.toString());
                reader.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
